package com.juyu.miliao.api.rabbitMQ;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.api.handler.IntegralRewardsAndPunishmentsHandler;
import com.juyu.miliao.common.constant.IntegralMqKey;
import com.juyu.miliao.common.domain.dto.SystemRewardsAndPunishmentDTO;
import com.juyu.miliao.common.domain.dto.UploadPicDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xuanqicheng
 */
@Component
@Slf4j
public class IntegralMq {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private IntegralRewardsAndPunishmentsHandler integralRewardsAndPunishmentsHandler;

    /**
     * 成功发布动态
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(IntegralMqKey.SuccessReleaseDynamic.QUEUE_NAME),
            exchange = @Exchange(IntegralMqKey.EXCHANGE),
            key = IntegralMqKey.SuccessReleaseDynamic.ROUTING_KEY))
    public void getSuccessReleaseDynamic(String message) {
        log.info("处理动态增加积分message=[{}]",message);
        if (!StringUtils.isEmpty(message)) {
            try {
                integralRewardsAndPunishmentsHandler.successReleaseOfADynamic(Integer.parseInt(message));
            } catch (Exception ex) {
                log.error("处理动态增加积分据错误,message=[{}],ex=[{}]",message,ex.getMessage());
            }
        }
    }


    /**
     * 上传图片处理积分
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(IntegralMqKey.UploadPic.QUEUE_NAME),
            exchange = @Exchange(IntegralMqKey.EXCHANGE),
            key = IntegralMqKey.UploadPic.ROUTING_KEY))
    public void getUploadPic(String message) {
        log.info("处理上传图片增加积分:message=[{}]",message);
        if (!StringUtils.isEmpty(message)) {
            try {
                UploadPicDTO uploadPicDTO = objectMapper.readValue(message,UploadPicDTO.class);
                integralRewardsAndPunishmentsHandler.uploadPic(uploadPicDTO.getUserId(),uploadPicDTO.getNum());
            } catch (Exception ex) {
                log.error("处理上传图片增加积分据错误,message=[{}],ex=[{}]",message ,ex.getMessage());
            }
        }
    }


    /**
     * 上传视频处理积分
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(IntegralMqKey.UploadVideo.QUEUE_NAME),
            exchange = @Exchange(IntegralMqKey.EXCHANGE),
            key = IntegralMqKey.UploadVideo.ROUTING_KEY))
    public void getUploadVideo(String message) {
        log.info("处理上传视频增加积分:userId=[{}]",message);
        if (!StringUtils.isEmpty(message)) {
            try {
                integralRewardsAndPunishmentsHandler.uploadVideo(Long.parseLong(message));
            } catch (Exception ex) {
                log.error("处理上传视频增加积分据错误,message=[{}],ex=[{}]",message ,ex.getMessage());
            }
        }
    }


    /**
     * 系统奖励与惩罚
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(IntegralMqKey.SystemRewardsAndPunishment.QUEUE_NAME),
            exchange = @Exchange(IntegralMqKey.EXCHANGE),
            key = IntegralMqKey.SystemRewardsAndPunishment.ROUTING_KEY))
    public void getSystemRewardsAndPunishment(String message) {
        log.info("处理系统奖励惩罚积分:userId=[{}]",message);
        if (!StringUtils.isEmpty(message)) {
            try {
                SystemRewardsAndPunishmentDTO dto = objectMapper.readValue(message,
                        SystemRewardsAndPunishmentDTO.class);
                integralRewardsAndPunishmentsHandler.getSystemRewardsAndPunishment(dto);
            } catch (Exception ex) {
                log.error("处理系统奖励惩罚积分错误,message=[{}],ex=[{}]",message ,ex.getMessage());
            }
        }
    }

}
